import ResUrl from "../Res/ResUrl";
import CommonSaveData from "../Commom/CommonSaveData";
import { ESoundName } from "../../script/enum/ESound";


export default class AudioManager {
    private _bgPast: string[] = [];
    private _urlBGM: string = '';
    private _urlSOUND: string = '';

    public static get instance(): AudioManager {
        if (this._instance == null) {
            this._instance = new AudioManager();
        }
        return this._instance;
    }
    private static _instance: AudioManager;

    private constructor() { }

    public Preload(urls: string[]) {
        urls.push(ResUrl.music_url(ESoundName.bgm));
    }

    public setMusicOpen(bool: boolean) {
        CommonSaveData.instance.isMusicOn = bool;
        if (bool)
            fgui.UIConfig.buttonSound = ResUrl.sound_url(ESoundName.choose);
        else
            fgui.UIConfig.buttonSound = "";
        CommonSaveData.SaveToDisk();
        if (bool) {
            this.playBGM(ESoundName.bgm);
        }
        else {
            this.pauseBGM();
        }
    }

    public setSoundOpen(bool: boolean) {
        CommonSaveData.instance.isSoundOn = bool;
        CommonSaveData.SaveToDisk();
        Laya.SoundManager.soundMuted = !bool;
        if (!bool) {
            this.pauseSound();
        }
    }

    public playBGM(name: string): void {
        if (name != null && this._bgPast.slice(-1)[0] != name) {
            this._bgPast.push(name);
            this._urlBGM = ResUrl.music_url(name);
            this._playMusic();
            console.log("播放背景音乐1", name);
        }
        else {
            if (this._urlBGM != "") {
                this._playMusic();
                console.log("播放背景音乐2", name);
            } else {
            }
        }
    }

    public shiftBGM(name: string): void {
        if (this._bgPast.slice(-1)[0] == name) {
            this._bgPast.pop();
            let pastBg: string = this._bgPast.slice(-1)[0];
            if (pastBg) {
                this._urlBGM = ResUrl.music_url(name);
                this._playMusic();
            }
        }
    }

    public pauseBGM() {
        Laya.SoundManager.stopMusic();
        console.log("停止播放音乐", this._urlBGM);
    }

    public pauseSound() {
        Laya.SoundManager.stopAllSound();
    }

    private _sounds: Laya.SoundChannel[] = [];
    public playSound(type: string, loops?: number, complete?: Laya.Handler, soundClass?: any, startTime?: number): void {
        this._urlSOUND = ResUrl.sound_url(type);
        if (CommonSaveData.instance.isSoundOn) {
            for (let i = 0; i < this._sounds.length; i++) {
                if (this._sounds[i]) {
                    if (this._sounds[i].url.indexOf(this._urlSOUND) >= 0) {
                        this._sounds[i].stop();
                        this._sounds.splice(i, 1);
                        break;
                    }
                }
            }
            let temp = Laya.SoundManager.playSound(this._urlSOUND, loops, complete, soundClass, startTime);
            this._sounds.push(temp);
        }
    }

    public stopSound(type: string): void {
        this._urlSOUND = ResUrl.sound_url(type);
        Laya.SoundManager.stopSound(this._urlSOUND);
    }

    private _playMusic() {
        if (CommonSaveData.instance.isMusicOn) {
            Laya.SoundManager.stopMusic();
            Laya.SoundManager.playMusic(this._urlBGM, 0);
        } else {
        }
    }
}